export function listToTree(data) {
	let result = [];
	let map = {};
	data.forEach(item => {
		map[item.id] = item;
	});
	data.forEach(item => {
		let parent = map[item.parentId];
		if (parent) {
			(parent.children || (parent.children = [])).push(item);
		} else {
			result.push(item);
		}
	});
	return result;
}

let result = null // 报错最终结果
export function findNodeFromTreeById(root, id) {
	if (!!root) {
		let type = Object.prototype.toString.call(root).slice(8, -1)
		if (type === 'Object') {
			if (root.id && root.id === id) {
				result = root
			} else {
				let node = root.children || null
				findNodeFromTreeById(node, id)
			}
		} else if (type === 'Array') {
			let needNode = root.find(x => !!x === true && x.id === id)
			if (!!needNode) {
				result = needNode
			} else {
				root && root.forEach(item => {
					if (item && item.children && item.children.length) {
						findNodeFromTreeById(item.children, id)
					}
				})
			}
		}
	}
	return result
}